// action ->  mutation -> state
import * as types from '../types'
import { storeExpire } from '@/config/index'

const state = {
  sidebar: {
    opened: true
  },
  theme: 'default',
  fullscreenLoading: false,
  fullscreenLoadingTxt: "请稍候...",
  visitedViews: []
}

const mutations = {
  [types.SET_FULLSCREEN_LOADING](state, {isShow, txt}){
    state.fullscreenLoading = isShow;
    state.fullscreenLoadingTxt = txt || "请稍候...";
  },
  [types.TOGGLE_SIDEBAR](state, { isOpen }){
    state.sidebar.opened = isOpen;
  },
  [types.ADD_VISITED_VIEWS]: (state, {view}) => {
    if (state.visitedViews.some(v => v.path === view.path)) return
    state.visitedViews.push({ name: view.name, path: view.path })
  },
  [types.DEL_VISITED_VIEWS]: (state, {view}) => {
    let index
    for (const [i, v] of state.visitedViews.entries()) {
      if (v.path === view.path) {
        index = i
        break
      }
    }
    state.visitedViews.splice(index, 1)
  }
};

const actions = {
  setFullscreenLoading({commit}, data){
    commit(types.SET_FULLSCREEN_LOADING, data);
  },
  ToggleSideBar: ({ commit, state }) => {
    commit(types.TOGGLE_SIDEBAR, { __store: { expire: storeExpire }, isOpen: !state.sidebar.opened })
  },
  addVisitedViews: ({ commit }, view) => {
    commit(types.ADD_VISITED_VIEWS, { view })
  },
  delVisitedViews: ({ commit }, view) => {
    commit(types.DEL_VISITED_VIEWS, { view })
  }
}

export default {
  state,
  mutations,
  actions
}
